using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Preferences;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace NorthStar.Driver
{
    [Activity(Label = "Preferences")]
    public class PreferencesActivity : Activity
    {
        private ISharedPreferences _preferences;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Preferences);

            _preferences = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);

            string phoneNumber = _preferences.GetString("phoneNumber", "04112345678");
            string imei = _preferences.GetString("imei", "ABC12345");

            string server = _preferences.GetString("server", "http://192.168.1.1/TheNorthStar");
            EditText phoneTxt = FindViewById<EditText>(Resource.Id.Phone_input);
            EditText imeiTxt = FindViewById<EditText>(Resource.Id.IMEI_input);
            EditText serverTxt = FindViewById<EditText>(Resource.Id.server_input);

            phoneTxt.Text = phoneNumber;
            imeiTxt.Text = imei;
            serverTxt.Text = server;
            Button saveBtn = FindViewById<Button>(Resource.Id.btnSavePreferences);
            saveBtn.Click += (sender, e) =>
                                 {
                                     ISharedPreferencesEditor editor = _preferences.Edit();
                                     editor.PutString("phoneNumber", phoneTxt.Text);
                                     editor.PutString("imei", imeiTxt.Text);
                                     editor.PutString("server", serverTxt.Text);
                                     editor.Commit();
                                     Intent a = new Intent(this, typeof(Activity1));
                                     a.AddFlags(ActivityFlags.ClearTop);
                                     StartActivity(a);
                                 };
            // Create your application here
        }
    }
}